<template>
  <div class="app-container">
    <PropTable
      ref="tableData"
      :loading="loading"
      :columns="column"
      :data="data"
      @reset="reset"
      @onSubmit="onSubmit"
      @change="changeFileState"
      :unadd="false"
    >
      <template v-slot:operation="scope">
        <el-button
          v-if="
            scope.row.fileType.toLowerCase() === 'pdf' ||
            scope.row.fileType.toLowerCase() === 'jpg' ||
            scope.row.fileType.toLowerCase() === 'png'
          "
          type="primary"
          size="small"
          icon="View"
          @click="viewPdf(scope.row)"
        >
          预览
        </el-button>
        <el-button v-else type="primary" size="small" icon="Download" @click="download(scope.row)">
          下载
        </el-button>
      </template>
    </PropTable>
    <ViewFileDrawer ref="fileViewDrawer" />
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, ref } from 'vue'
  import PropTable from '@/components/Table/PropTable/index.vue'
  import ViewFileDrawer from '@/components/fileViewDrawer/index.vue'
  import { postFileDetailAllSearch } from '@/api/controller/file-detail-controller/index'
  import { ElMessage } from 'element-plus'
  import {
    getFileDetailById,
    putFileDetailUpdate,
  } from '@/api/controller/file-detail-controller/index'
  const pageInfo = ref({})
  const tableData = ref([])
  const loading = ref(true)
  const column = [
    { name: 'type', label: '文件类型', inSearch: true, valueType: 'input', width: 140 },
    { name: 'stage', label: '文件分类', inSearch: true, valueType: 'input', width: 140 },
    { name: 'title', label: '文件标题', inSearch: true, valueType: 'input' },
    { name: 'projectBrief', label: '所属项目简称', inSearch: true, valueType: 'input' },
    { name: 'isDelete', label: '是否作废', itemIf: true, width: 110 },
    { name: 'remark', label: '备注', inSearch: true, valueType: 'input' },
    { name: 'operation', slot: true, fixed: 'right', width: 110, label: '操作', type: false },
  ]
  const data = ref(null)
  const loadData = async (val) => {
    let res = await postFileDetailAllSearch(val)
    data.value = res.data
  }

  //文件预览抽屉
  const fileViewDrawer = ref()
  const viewPdf = async (data) => {
    let res = await getFileDetailById({ id: data.id })
    fileViewDrawer.value.show(res.data)
  }
  //文件下载
  const download = async (row) => {
    let res = await getFileDetailById({ id: row.id })
    window.open(res.data.filePath)
  }
  //重置搜索条件
  const reset = async () => {
    try {
      loading.value = true
      pageInfo.value = {
        pageNum: 1,
        pageSize: 15,
      }
      await loadData(pageInfo.value)
    } finally {
      loading.value = false
    }
  }
  //搜索
  const onSubmit = async (val) => {
    try {
      loading.value = true
      await loadData(val)
    } finally {
      loading.value = false
    }
  }
  //初始化
  onMounted(async () => {
    try {
      loading.value = true
      pageInfo.value = {
        pageNum: 1,
        pageSize: 15,
      }
      await loadData(pageInfo.value)
    } finally {
      loading.value = false
    }
  })

  //改变文件状态
  const changeFileState = async (id, isDelete, str) => {
    const data = {
      id: id,
      isDelete: isDelete,
    }
    const res = await putFileDetailUpdate(data, { loading: true })
    if (res.code === 200) {
      if (isDelete) {
        ElMessage.success('作废成功')
      } else {
        ElMessage.success('启用成功')
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    flex: 1;
    display: flex;
    width: 100%;
    padding: 16px;
    box-sizing: border-box;
  }
</style>
